﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using XChipTool.Enties;
using System.IO;
using XChip_Common;

namespace XChipTool
{
    public partial class XChip_SettingFileTVE : DevExpress.XtraEditors.XtraForm
    {
        public bincurrencyManager Bincurrency_BLL;
        private string GuidNo;
        private int Id = -1;
        private bincurrency_export BinModel;
        public delegate void SendEventBinSettingNewMsg(string msg);
        public event SendEventBinSettingNewMsg SendBinSetMsg;
        public List<string> OSDlist;
        public XChip_SettingFileTVE()
        {
            InitializeComponent();
        }
        public XChip_SettingFileTVE(bincurrency_export binmodel)
        {
            Id = binmodel.Id;
            GuidNo = binmodel.GuidId;
            BinModel = binmodel;
            InitializeComponent();
        }
        private void XChip_SettingFileTVE_Load(object sender, EventArgs e)
        {
            if (null == Bincurrency_BLL) //初始化
            {
                Bincurrency_BLL = new bincurrencyManager();

            }
           
            navigationPane1.SelectedPage = Setting1;
            navigationPane1.SelectedPage = datapath;
            List<TreeListValue> listC = new List<TreeListValue>();
            OSDlist = new List<string>();
            //添加分页地址
            OSDlist.Add("0xfffd,0x20,");
            OSDlist.Add("0xfffe,0x00,");
            Task.Run(() => {

                this.Invoke((EventHandler)delegate {
                    for (int i = 0; i < 48; i++)
                    {
                        listC.Add(new TreeListValue { ValueMember = i.ToString("X2"), DisplayMember = i.ToString("X2") });
                    }
                    bianhaoLook.Properties.DataSource = listC;
                    bianhaoLook.ItemIndex = 0;

                    //增加提示

                    for (int i = 0; i < 48; i++)
                    {

                        try
                        {
                            Control col = this.Controls.Find($"txtlabSetting{i + 1}", true)[0];
                            if (!string.IsNullOrEmpty(SettingTo151_Help.SetTips151[i].Tips))
                            {
                                col.Text = SettingTo151_Help.SetTips151[i].Tips;
                            }
                            else
                            {
                                Control datapathcol = this.Controls.Find($"datapath{i + 1}", true)[0];
                                //datapathcol.Visible = false;
                                col.Text = $"Setting{i}";
                            }
                        }
                        catch (Exception ex)
                        {

                            throw;
                        }
                    }

                    for (int i = 50; i < 82; i++)
                    {
                        try
                        {
                            Control col = this.Controls.Find($"txtlabSetting{i}", true)[0];
                            col.Text = $"TVE_{i - 1}";

                        }
                        catch (Exception ex)
                        {

                            throw;
                        }
                    }
                    for (int i = 82; i < 88; i++)
                    {
                        try
                        {
                            Control col = this.Controls.Find($"txtlabSetting{i}", true)[0];
                            col.Text = $"IQ_setting";

                        }
                        catch (Exception ex)
                        {

                            throw;
                        }
                    }
                    for (int i = 88; i < 100; i++)
                    {
                        try
                        {
                            Control col = this.Controls.Find($"txtlabSetting{i}", true)[0];
                            col.Text = $"sensor_{i - 1}";

                        }
                        catch (Exception ex)
                        {

                            throw;
                        }
                    }
                });
            });
            



            var list = Bincurrency_BLL.GetListToDefault(GuidNo);
            var model00 = list.Where(c => (c.BinId == 6 || c.BinId == 7 || c.BinId == 8 || c.BinId == 9) && c.BinInPath != null);
            foreach (var item in model00)
            {
                //var idex = SettingList_Help.SetTips128.FindIndex(c => c.SettingNo == item.SerialNo.ToString());
                SetSettingPath((int)item.SerialNo + 1, new List<SettingFile> { new SettingFile { FilePath = item.BinInPath, FileName = item.BinInName } });
            }
            var model99 = list.Find(c => (c.BinId == 7 && c.SerialNo == 48) && c.BinInPath != null);
            if (model99 != null)
            {
                //isloadAgain = true;
                //commonList = new List<TreeListValue>();
                //commonList.Add(new TreeListValue { ValueMember = model99.BinInPath, DisplayMember = model99.BinInName });
                commonTxt.Properties.DataSource = new List<SettingFile> { new SettingFile { FilePath = model99.BinInPath, FileName = model99.BinInName } };
                commonTxt.ItemIndex = 0;
                //isloadAgain = false;
            }
        }
        private void SetSettingPath(int num, List<SettingFile> data)
        {
           
            //Setting1
            if (num == int.Parse(settingPathSetting1.Tag.ToString()))
            {
                settingPathSetting1.Properties.DataSource = data; settingPathSetting1.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting2.Tag.ToString()))
            {
                settingPathSetting2.Properties.DataSource = data; settingPathSetting2.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting3.Tag.ToString()))
            {
                settingPathSetting3.Properties.DataSource = data; settingPathSetting3.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting4.Tag.ToString()))
            {
                settingPathSetting4.Properties.DataSource = data; settingPathSetting4.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting5.Tag.ToString()))
            {
                settingPathSetting5.Properties.DataSource = data; settingPathSetting5.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting6.Tag.ToString()))
            {
                settingPathSetting6.Properties.DataSource = data; settingPathSetting6.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting7.Tag.ToString()))
            {
                settingPathSetting7.Properties.DataSource = data; settingPathSetting7.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting8.Tag.ToString()))
            {
                settingPathSetting8.Properties.DataSource = data; settingPathSetting8.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting9.Tag.ToString()))
            {
                settingPathSetting9.Properties.DataSource = data; settingPathSetting9.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting10.Tag.ToString()))
            {
                settingPathSetting10.Properties.DataSource = data; settingPathSetting10.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting11.Tag.ToString()))
            {
                settingPathSetting11.Properties.DataSource = data; settingPathSetting11.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting12.Tag.ToString()))
            {
                settingPathSetting12.Properties.DataSource = data; settingPathSetting12.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting13.Tag.ToString()))
            {
                settingPathSetting13.Properties.DataSource = data; settingPathSetting13.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting14.Tag.ToString()))
            {
                settingPathSetting14.Properties.DataSource = data; settingPathSetting14.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting15.Tag.ToString()))
            {
                settingPathSetting15.Properties.DataSource = data; settingPathSetting15.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting16.Tag.ToString()))
            {
                settingPathSetting16.Properties.DataSource = data; settingPathSetting16.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting17.Tag.ToString()))
            {
                settingPathSetting17.Properties.DataSource = data; settingPathSetting17.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting18.Tag.ToString()))
            {
                settingPathSetting18.Properties.DataSource = data; settingPathSetting18.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting19.Tag.ToString()))
            {
                settingPathSetting19.Properties.DataSource = data; settingPathSetting19.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting20.Tag.ToString()))
            {
                settingPathSetting20.Properties.DataSource = data; settingPathSetting20.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting21.Tag.ToString()))
            {
                settingPathSetting21.Properties.DataSource = data; settingPathSetting21.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting22.Tag.ToString()))
            {
                settingPathSetting22.Properties.DataSource = data; settingPathSetting22.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting23.Tag.ToString()))
            {
                settingPathSetting23.Properties.DataSource = data; settingPathSetting23.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting24.Tag.ToString()))
            {
                settingPathSetting24.Properties.DataSource = data; settingPathSetting24.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting25.Tag.ToString()))
            {
                settingPathSetting25.Properties.DataSource = data; settingPathSetting25.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting26.Tag.ToString()))
            {
                settingPathSetting26.Properties.DataSource = data; settingPathSetting26.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting27.Tag.ToString()))
            {
                settingPathSetting27.Properties.DataSource = data; settingPathSetting27.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting28.Tag.ToString()))
            {
                settingPathSetting28.Properties.DataSource = data; settingPathSetting28.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting29.Tag.ToString()))
            {
                settingPathSetting29.Properties.DataSource = data; settingPathSetting29.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting30.Tag.ToString()))
            {
                settingPathSetting30.Properties.DataSource = data; settingPathSetting30.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting31.Tag.ToString()))
            {
                settingPathSetting31.Properties.DataSource = data; settingPathSetting31.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting32.Tag.ToString()))
            {
                settingPathSetting32.Properties.DataSource = data; settingPathSetting32.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting33.Tag.ToString()))
            {
                settingPathSetting33.Properties.DataSource = data; settingPathSetting33.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting34.Tag.ToString()))
            {
                settingPathSetting34.Properties.DataSource = data; settingPathSetting34.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting35.Tag.ToString()))
            {
                settingPathSetting35.Properties.DataSource = data; settingPathSetting35.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting36.Tag.ToString()))
            {
                settingPathSetting36.Properties.DataSource = data; settingPathSetting36.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting37.Tag.ToString()))
            {
                settingPathSetting37.Properties.DataSource = data; settingPathSetting37.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting38.Tag.ToString()))
            {
                settingPathSetting38.Properties.DataSource = data; settingPathSetting38.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting39.Tag.ToString()))
            {
                settingPathSetting39.Properties.DataSource = data; settingPathSetting39.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting40.Tag.ToString()))
            {
                settingPathSetting40.Properties.DataSource = data; settingPathSetting40.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting41.Tag.ToString()))
            {
                settingPathSetting41.Properties.DataSource = data; settingPathSetting41.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting42.Tag.ToString()))
            {
                settingPathSetting42.Properties.DataSource = data; settingPathSetting42.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting43.Tag.ToString()))
            {
                settingPathSetting43.Properties.DataSource = data; settingPathSetting43.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting44.Tag.ToString()))
            {
                settingPathSetting44.Properties.DataSource = data; settingPathSetting44.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting45.Tag.ToString()))
            {
                settingPathSetting45.Properties.DataSource = data; settingPathSetting45.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting46.Tag.ToString()))
            {
                settingPathSetting46.Properties.DataSource = data; settingPathSetting46.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting47.Tag.ToString()))
            {
                settingPathSetting47.Properties.DataSource = data; settingPathSetting47.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting48.Tag.ToString()))
            {
                settingPathSetting48.Properties.DataSource = data; settingPathSetting48.ItemIndex = 0;
            }
            //Setting1
           
            else if (num == int.Parse(settingPathSetting1_18.Tag.ToString()))
            {
                settingPathSetting1_18.Properties.DataSource = data; settingPathSetting1_18.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting1_19.Tag.ToString()))
            {
                settingPathSetting1_19.Properties.DataSource = data; settingPathSetting1_19.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting1_20.Tag.ToString()))
            {
                settingPathSetting1_20.Properties.DataSource = data; settingPathSetting1_20.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting1_21.Tag.ToString()))
            {
                settingPathSetting1_21.Properties.DataSource = data; settingPathSetting1_21.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting1_22.Tag.ToString()))
            {
                settingPathSetting1_22.Properties.DataSource = data; settingPathSetting1_22.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting1_23.Tag.ToString()))
            {
                settingPathSetting1_23.Properties.DataSource = data; settingPathSetting1_23.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting1_24.Tag.ToString()))
            {
                settingPathSetting1_24.Properties.DataSource = data; settingPathSetting1_24.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting1_25.Tag.ToString()))
            {
                settingPathSetting1_25.Properties.DataSource = data; settingPathSetting1_25.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting1_26.Tag.ToString()))
            {
                settingPathSetting1_26.Properties.DataSource = data; settingPathSetting1_26.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting1_27.Tag.ToString()))
            {
                settingPathSetting1_27.Properties.DataSource = data; settingPathSetting1_27.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting1_28.Tag.ToString()))
            {
                settingPathSetting1_28.Properties.DataSource = data; settingPathSetting1_28.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting1_29.Tag.ToString()))
            {
                settingPathSetting1_29.Properties.DataSource = data; settingPathSetting1_29.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting1_30.Tag.ToString()))
            {
                settingPathSetting1_30.Properties.DataSource = data; settingPathSetting1_30.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting1_31.Tag.ToString()))
            {
                settingPathSetting1_31.Properties.DataSource = data; settingPathSetting1_31.ItemIndex = 0;
            }
            else if (num == int.Parse(settingPathSetting1_32.Tag.ToString()))
            {
                settingPathSetting1_32.Properties.DataSource = data; settingPathSetting1_32.ItemIndex = 0;
            }
            
        }

        private void settingPathSetting1_EditValueChanged(object sender, EventArgs e)
        {
            var settingPath = sender as LookUpEdit;
            BinUpdate(settingPath.Text.ToString(), settingPath.Tag.ToString());
        }
        private void BinUpdate(string pathName, string tag)
        {
            if (string.IsNullOrEmpty(pathName) || pathName == "请选择文件") return;

            var list = Bincurrency_BLL.GetList(c => c.GuidId == BinModel.GuidId && (c.BinId == 6 || c.BinId == 7 || c.BinId == 8 || c.BinId == 9) && c.SerialNo != 48);
            //var list = Bincurrency_BLL.GetList(c => c.GuidId == BinModel.GuidId && (c.BinId == 6 || c.BinId == 7 || c.BinId == 8 || c.BinId == 9));
            var modelMax = list.Max(c => c.SortOrder) == null ? 0 : list.Max(c => c.SortOrder);
            var tagTemp = (int.Parse(tag) - 1);
            var modelT = SettingTo151_Help.SetTips151[tagTemp];
            uint settingHex = 0;
            if (string.IsNullOrEmpty(modelT.TransferHex) || string.IsNullOrEmpty(modelT.VideoHex))
            {
                settingHex = Convert.ToUInt32($"{tagTemp.ToString("X2")}000000", 16);
            }
            else
            {
                settingHex = Convert.ToUInt32($"{tagTemp.ToString("X2")}00{modelT.TransferHex.Replace("0x", "")}{modelT.VideoHex.Replace("0x", "")}", 16);
            }
            //// 处理文件名称 相同文件 根据Tag值进行区分 用与Flash生成需要添加不同的内容
            //string FileName = System.IO.Path.GetFileName(pathName); //获取文件名
            //string saveName = System.IO.Path.GetDirectoryName(pathName);
            if (!list.Exists(c => c.SerialNo == tagTemp))
            {
                bincurrency model = new bincurrency();
                model.GuidId = GuidNo;
                model.BinId = (tagTemp / 32) + 6;
                model.CreateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                model.BinInPath = pathName;
                model.BinInName = Path.GetFileName(pathName);

                var pathNameout = Copy_Files_For_Header_Patch_Tool(pathName, true, "", true, false, settingHex);
                model.BinOutName = Path.GetFileName(pathNameout);
                model.BinOutPath = pathNameout;
                model.FileStatus = 2;
                model.SerialNo = tagTemp;
                //model.SortOrder = modelMax + 1;
                model.SortOrder = tagTemp;
                model.IsModified = 0;
                model.LengthAddress = HeaderHelper.FilePathLengthComplementnew(model.BinOutPath);
                Bincurrency_BLL.Insert(model);
            }
            else
            {
                var model = list.Find(c => c.SerialNo == tagTemp);
                model.BinInPath = pathName;
                model.BinInName = Path.GetFileName(pathName);
                var pathNameout = Copy_Files_For_Header_Patch_Tool(pathName, true, "", true, false, settingHex);
                model.BinOutName = Path.GetFileName(pathNameout);
                model.BinOutPath = pathNameout;
                model.SerialNo = tagTemp;
                model.FileStatus = 2;
                model.LengthAddress = HeaderHelper.FilePathLengthComplementnew(model.BinOutPath);
                Bincurrency_BLL.Update(model);
            }
            var mode = list.Find(c => c.IsModified == 1);
            mode.FileStatus = 2;
            Bincurrency_BLL.Update(mode);
        }
        /// <summary>
        /// 拷贝文件 //生成批处理文本//生成批处理文件//执行批处理文件//生成完成
        /// </summary>
        /// <param name="Inputfilepath">文本路径</param>
        /// <param name="Isflash">是flash版本还eeprom版本</param>
        /// <param name="eepromaddr">eeprom值</param>
        /// <param name="isSetting">区分是setting栏还是其他栏</param>
        public string Copy_Files_For_Header_Patch_Tool(string Inputfilepath, bool Isflash, string eepromaddr, bool isSetting, bool isHead, uint setHex = 0x0)
        {
            string strFileName = System.IO.Path.GetFileName(Inputfilepath); //获取文件名
            string strNewSavePath = Application.StartupPath + "\\Lib\\xc151_setting\\Flash_" + setHex.ToString("X2") + strFileName;
            //判断该路径下文件是否存在
            if (File.Exists(strNewSavePath))
            {
                //如果存在文件就删除
                File.Delete(strNewSavePath);
            }
            if (isHead)
            {
                Setting2FlashE2prom.FlashHeader2Flash(Inputfilepath, strNewSavePath);
            }
            else
            {
                //Setting2FlashE2prom.FlashSetting2Flash(Inputfilepath, strNewSavePath, 1, 0, 0);
                Setting2FlashE2prom.FlashSetting2Flash(Inputfilepath, strNewSavePath, 0, 0, 0, 0, 0, setHex);
            }

            return strNewSavePath;
        }

        private void setbtnsetting1_Click(object sender, EventArgs e)
        {
            var btn = sender as DevExpress.XtraEditors.SimpleButton;
            try
            {
                var serialno = (int.Parse(btn.Tag.ToString()) - 1);
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "TXT Files(*.txt;*.c)|*.txt;*.c|All Files(*.*)|*.*";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    DirectoryInfo parentDirectory = Directory.GetParent(openFileDialog1.FileName);
                    string foldPath = parentDirectory.FullName;
                    AppHelper.UpdateAppConfig("Folder_Road_Force", foldPath);
                    #region 32套文件勾选
                    Control col = this.Controls.Find($"txtlabSetting{btn.Tag.ToString()}", true)[0];
                    var aaa = SettingTo151_Help.SetTips151.Find(c => c.Tips == col.Text);
                    var HexOutSplit = aaa.HexOut.Split(',');
                    if (OSDlist.Where(c => c.Contains(HexOutSplit[2])).ToList().Count > 0)
                    {
                        OSDlist.Remove(OSDlist.Find(c => c.Contains(HexOutSplit[2])));
                    }
                    OSDlist.Add(HexOutSplit[0]+","+ aaa.VideoHex+","+ HexOutSplit[2]);
                    switch (btn.Tag.ToString())
                    {
                        case "1":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting1);
                            }
                            break;
                        case "2":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting2);
                            }
                            break;
                        case "3":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting3);
                            }
                            break;
                        case "4":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting4);
                            }
                            break;
                        case "5":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting5);
                            }
                            break;
                        case "6":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting6);
                            }
                            break;
                        case "7":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting7);
                            }
                            break;
                        case "8":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting8);
                            }
                            break;
                        case "9":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting9);
                            }
                            break;
                        case "10":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting10);
                            }
                            break;
                        case "11":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting11);
                            }
                            break;
                        case "12":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting12);
                            }
                            break;
                        case "13":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting13);
                            }
                            break;
                        case "14":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting14);
                            }
                            break;
                        case "15":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting15);
                            }
                            break;
                        case "16":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting16);
                            }
                            break;
                        case "17":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting17);
                            }
                            break;
                        case "18":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting18);
                            }
                            break;
                        case "19":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting19);
                            }
                            break;
                        case "20":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting20);
                            }
                            break;
                        case "21":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting21);
                            }
                            break;
                        case "22":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting22);
                            }
                            break;
                        case "23":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting23);
                            }
                            break;
                        case "24":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting24);
                            }
                            break;
                        case "25":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting25);
                            }
                            break;
                        case "26":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting26);
                            }
                            break;
                        case "27":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting27);
                            }
                            break;
                        case "28":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting28);
                            }
                            break;
                        case "29":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting29);
                            }
                            break;
                        case "30":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting30);
                            }
                            break;
                        case "31":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting31);
                            }
                            break;
                        case "32":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting32);
                            }
                            break;
                        case "33":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting33);
                            }
                            break;
                        case "34":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting34);
                            }
                            break;
                        case "35":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting35);
                            }
                            break;
                        case "36":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting36);
                            }
                            break;
                        case "37":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting37);
                            }
                            break;
                        case "38":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting38);
                            }
                            break;
                        case "39":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting39);
                            }
                            break;
                        case "40":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting40);
                            }
                            break;
                        case "41":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting41);
                            }
                            break;
                        case "42":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting42);
                            }
                            break;
                        case "43":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting43);
                            }
                            break;
                        case "44":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting44);
                            }
                            break;
                        case "45":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting45);
                            }
                            break;
                        case "46":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting46);
                            }
                            break;
                        case "47":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting47);
                            }
                            break;
                        case "48":
                            {
                                SetLookEdit(openFileDialog1, serialno, settingPathSetting48);
                            }
                            break;
                        default:
                            break;
                    }
                    #endregion
                }
            }
            catch
            {
                WaitBefore.Close();
                GenerateFileHelp.Close_Process_ByAll();
            }
        }
        private static void SetLookEdit(OpenFileDialog openFileDialog1, int Num, DevExpress.XtraEditors.LookUpEdit setPath)
        {
            if (null == SettingTo151_Help.SetTips151[Num].data)
            {
                SettingTo151_Help.SetTips151[Num].data = new List<SettingFile>();
            }
            SettingTo151_Help.SetTips151[Num].data.Add(new SettingFile() { FilePath = openFileDialog1.FileName, FileName = Path.GetFileName(openFileDialog1.FileName) });
            setPath.Properties.DataSource = SettingTo151_Help.SetTips151[Num].data;
            setPath.ItemIndex = SettingTo151_Help.SetTips151[Num].data.Count() - 1;

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (null != SendBinSetMsg)
            {
                SendBinSetMsg("");
                var model99 = Bincurrency_BLL.GetSingle(c => c.GuidId == GuidNo && c.SerialNo == 48 && c.BinId == 7);
                if (model99 == null)
                {
                    XtraMessageBox.Show("请导入common_setting");
                    return;
                }
                //判断该路径下文件是否存在
                if (File.Exists(Application.StartupPath + "\\Lib\\xc151_setting\\XChipCommonSetting.txt"))
                {
                    //如果存在文件就删除
                    File.Delete(Application.StartupPath + "\\Lib\\xc151_setting\\XChipCommonSetting.txt");
                }
                //复制一份数据
                File.Copy(model99.BinInPath, Application.StartupPath + "\\Lib\\xc151_setting\\XChipCommonSetting.txt");
                File.AppendAllLines(Application.StartupPath + "\\Lib\\xc151_setting\\XChipCommonSetting.txt", OSDlist);
                var aa =  Copy_Files_For_Header_Patch_Tool(Application.StartupPath + "\\Lib\\xc151_setting\\XChipCommonSetting.txt", true, "", true, false);
                model99.BinOutPath = aa;
                model99.BinOutName = Path.GetFileName(aa);
                model99.LengthAddress = HeaderHelper.FilePathLengthComplementnew(model99.BinOutPath);
                model99.CreateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                Bincurrency_BLL.Update(model99);
            }
            this.Close();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (null != SendBinSetMsg)
            {
                SendBinSetMsg("");
            }
            DialogResult dr = DevExpress.XtraEditors.XtraMessageBox.Show($"选择YES保存后退出！ \r\n 选择NO直接退出！", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
            if (dr == DialogResult.No)
            {
                this.Close();
            }
            else
            {
                this.Close();
            }
        }

        private void SelectFolderButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            dialog.SelectedPath = AppHelper.GetAppConfig("Folder_Road_Force");
            ///Folder_Road_Force
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                //清空下拉框数据源
                //ClearLookUpEditDataSource();
                string foldPath = dialog.SelectedPath;
                AppHelper.UpdateAppConfig("Folder_Road_Force", foldPath);
                ///*t*/extBox2.Text = foldPath;
                ///

                SetTipList();
            }
        }
        private void SetTipList()
        {
            var filePaths = Directory.GetFiles(AppHelper.GetAppConfig("Folder_Road_Force"), "*.txt").Concat(Directory.GetFiles(AppHelper.GetAppConfig("Folder_Road_Force"), "*.c")).ToArray();
            List<SettingFile> tlvList = new List<SettingFile>();
            foreach (var item in filePaths)
            {
                if (Path.GetFileName(item).Contains(" "))
                {
                    continue;
                }
                if (item.IndexOf(".bak", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    continue;
                }
                tlvList.Add(new SettingFile { FilePath = item, FileName = Path.GetFileName(item) });

            }
            var commonList = tlvList.Where(c => c.FileName.ToLower().Contains("common_setting")).ToList();
            if (commonList.Count() != 0)
            {
                commonTxt.Properties.DataSource = commonList;
                //commonTxt.ItemIndex = comList.Count() > 1 ? -1 : 0;//itemindex赋过正值后，无法赋成-1，原因未知
                //commonTxt.EditValue = null;
                commonTxt.EditValue = commonList.Count() > 1 ? string.Empty : commonList.First().FilePath;
                commonTxt.ItemIndex = 0;
            }
            //for (int i = 0; i < tlvList.Count; i++)
            //{
            //    var tag = int.Parse(SettingTo151_Help.SetTips151.Find(c => c.TransferName.ToLower() == tlvList[i].StandardName && c.VideoName.ToLower() == tlvList[i].TimingName).SettingNo);
            //    tlvList[i].Tag = tag;
            //}
            if (true)
            {
                //识别带制式的
                foreach (var item in tlvList)
                {
                    var tag = -1;
                    try
                    {
                        tag = int.Parse(SettingTo151_Help.SetTips151.Find(c => c.TransferName == item.StandardName && c.VideoName == item.TimingName).SettingNo) - 1;
                        var TransferName = SettingTo151_Help.SetTips151[tag].TransferName;
                        var VideoName = SettingTo151_Help.SetTips151[tag].VideoName;
                        if (tag != -1)
                        {
                            if (null != SettingTo151_Help.SetTips151[tag].data)
                            {
                                SettingTo151_Help.SetTips151[tag].data = new List<SettingFile>();
                                SettingTo151_Help.SetTips151[tag].data.AddRange(tlvList.Where(c => c.StandardName == TransferName && c.TimingName == VideoName).ToList());
                            }
                            else
                            {
                                SettingTo151_Help.SetTips151[tag].data = tlvList.Where(c => c.StandardName == TransferName && c.TimingName == VideoName).ToList();
                            }
                            if (!(null == SettingTo151_Help.SetTips151[tag].data || SettingTo151_Help.SetTips151[tag].data.Count() == 0))
                            {
                                var HexOutSplit = SettingTo151_Help.SetTips151[tag].HexOut.Split(',');
                                if (OSDlist.Where(c=>c.Contains(HexOutSplit[2])).ToList().Count > 0)
                                {
                                    OSDlist.Remove(OSDlist.Find(c => c.Contains(HexOutSplit[2])));
                                }
                                OSDlist.Add(HexOutSplit[0]+","+ SettingTo151_Help.SetTips151[tag].VideoHex +","+ HexOutSplit[2]);
                                SetSettingPath(tag + 1, SettingTo151_Help.SetTips151[tag].data);
                            }
                        }
                    }
                    catch (Exception)
                    {

                        continue;
                    }
                }
            }
            else
            {
                //识别X_[X]
                foreach (var item in tlvList.Where(c => c.Tag != -1))
                {
                    var tag = item.Tag;
                    if (null != SettingTo151_Help.SetTips151[tag].data)
                    {
                        SettingTo151_Help.SetTips151[tag].data = new List<SettingFile>();
                        SettingTo151_Help.SetTips151[tag].data.AddRange(tlvList.Where(c => c.Tag == tag).ToList());
                    }
                    else
                    {
                        SettingTo151_Help.SetTips151[tag].data = tlvList.Where(c => c.Tag == tag).ToList();
                    }
                    if (!(null == SettingTo151_Help.SetTips151[tag].data || SettingTo151_Help.SetTips151[tag].data.Count() == 0))
                    {
                        SetSettingPath(tag, SettingTo151_Help.SetTips151[tag].data);
                    }
                }
            }

        }

        private void commonTxt_EditValueChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(commonTxt.Text) && commonTxt.Text != "请选择文件")
            {
                //id 117  IsModified =1;
                var model99 = Bincurrency_BLL.GetSingle(c => c.GuidId == GuidNo && c.SerialNo == 48 && c.BinId == 7);
                var list = Bincurrency_BLL.GetList(c => c.GuidId == BinModel.GuidId && (c.BinId == 6 || c.BinId == 7 || c.BinId == 8 || c.BinId == 9));
                var modelMax = list.Max(c => c.SortOrder) == null ? 0 : list.Max(c => c.SortOrder);
                if (null == model99)
                {
                    var mode102 = new bincurrency();
                    mode102.GuidId = GuidNo;
                    mode102.BinId = 7;
                    mode102.CreateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    mode102.BinInPath = commonTxt.Text.ToString();
                    mode102.BinInName = Path.GetFileName(commonTxt.Text.ToString());
                    var pathNameout = Copy_Files_For_Header_Patch_Tool(commonTxt.Text.ToString(), true, "", true, false);
                    mode102.BinOutName = Path.GetFileName(pathNameout);
                    mode102.BinOutPath = pathNameout;
                    mode102.SerialNo = 48;
                    mode102.SortOrder = 48;
                    mode102.IsModified = 0;
                    mode102.LengthAddress = HeaderHelper.FilePathLengthComplementnew(mode102.BinOutPath);
                    Bincurrency_BLL.Insert(mode102);
                }
                else
                {
                    model99.BinInPath = commonTxt.Text.ToString();
                    model99.BinInName = Path.GetFileName(commonTxt.Text.ToString());
                    var pathNameout = Copy_Files_For_Header_Patch_Tool(commonTxt.Text.ToString(), true, "", true, false);
                    model99.BinOutName = Path.GetFileName(pathNameout);
                    model99.BinOutPath = pathNameout;
                    model99.LengthAddress = HeaderHelper.FilePathLengthComplementnew(model99.BinOutPath);
                    model99.CreateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    Bincurrency_BLL.Update(model99);
                }

            }
        }
    }
}